public class Solution {
    public int[] sortArray(int[] nums) {
        //归并排序
        int left = 0;
        int right = nums.length - 1;
        mergeSort(nums, left, right);
        return nums;
    }
    private void mergeSort(int[] nums, int left, int right){
        int mid = (left + right) / 2;
        //采用递归的思路分治
        mergeSort(nums, left, mid);
        mergeSort(nums, mid + 1, right);
        //合并两个有序数组
        merge(nums, left, mid, right);
    }
    private void merge(int[] nums, int left, int mid, int right){
        int[] tmpArr = new int[right - left + 1];
        //合并两个有序数组
        int s1 = left;
        int s2 = mid + 1;
        int index = 0;
        while(s1 <= mid && s2 <= right){
            if(nums[s1] < nums[s2]){
                tmpArr[index++] = nums[s1++];
            }else{
                tmpArr[index++] = nums[s2++];
            }
        }
        while(s1 <= mid){
            tmpArr[index++] = nums[s1++];
        }
        while(s2 <= right){
            tmpArr[index++] = nums[s2++];
        }
        for(int i = 0; i < index; i++){
            nums[i + left] = tmpArr[i];
        }
    }
}